<?php
/**
 * Created by PhpStorm.
 * User: RenBin
 * Date: 2015/10/8
 * Time: 15:03
 */

namespace common\models\Service;


use common\models\DAO\DayAccrual;
use common\models\DAO\FinanceAudit;
use common\models\DAO\FinanceCharge;
use common\models\DAO\FinanceRemainder;
use common\models\DAO\FinanceRemainderList;
use common\models\DAO\FinanceSendList;
use common\models\DAO\FinanceStaff;
use common\models\DAO\UserWalletLog;
use common\msg\UserMsg;
use yii\base\Exception;

class Pay {

    //发工资接口
    public static function send($staffID, $adminID, $companyID, $_salary = 0){
        $remainder = Wallet::remainder($companyID);
        $user = FinanceStaff::findOne($staffID);
        $error['status'] = 0;
        $error['msg'] = UserMsg::$time_out;
        if(!$user){
            $error['msg'] = UserMsg::$no_user;
            return $error;
        }
        $userID = $user['userID'];
        $userInfo =  $user->attributes;
        $transaction = \Yii::$app->db->beginTransaction();
        $init_money = $salary = intval($userInfo['salary']);
        if($_salary != 0 and $_salary != $salary){
            $init_money = $salary = $_salary;
            $user->salary = $_salary;
            $user_result = $user->save();
            if(!$user_result){
                $transaction->rollBack();
                return $error;
            }
        }
        if($salary <= 0){
            return ['status'=>1];
        }

        if($remainder < $salary){
            $error['msg'] = UserMsg::$no_money;
            return $error;
        }
        $now = time();
        $month = date('Ym');
        $audit = FinanceAudit::findOne(['staffID'=>$staffID, 'companyID'=>$companyID, 'date'=>$month]);
        if(!$audit){
            $error['msg'] = UserMsg::$no_audit;
            $transaction->rollBack();
            return $error;
        }
        try{
            $sendModel = new FinanceSendList();
            $sendModel->adminID = $adminID;
            $sendModel->userID = $userID;
            $sendModel->staffID = $staffID;
            $sendModel->createTime = $now;
            $sendModel->companyID = $companyID;
            $sendModel->money = $salary;
            $sendModel->month = $month;
            $send_result = $sendModel->save();
            if(!$send_result){
                $transaction->rollBack();
                return $error;
            }
        }catch (Exception $e){
            $transaction->rollBack();
            $error['msg'] = UserMsg::$already_user;
            return $error;
        }
        $accrual = FinanceRemainder::find()->andFilterWhere(['>', 'money', 0])->orderBy('createTime asc')->all();
        //财务账户扣款
        foreach($accrual as $v){
            $money = intval($v['money']);
            $id = $v['id'];
            if($money <= 0)
                continue;
            if($money < $init_money){
                $accrual_one = FinanceRemainder::findOne($id);
                $accrual_one->money = 0;
                $one_result = $accrual_one->save();
                if(!$one_result){
                    $transaction->rollBack();
                    return $error;
                }
                $investList = new FinanceRemainderList();
                $investList->staffID = $staffID;
                $investList->adminID = $adminID;
                $investList->remainderID = $id;
                $investList->sendID = $sendModel['id'];
                $investList->money = $money;
                $investList->createTime = $now;
                $investList->companyID = $companyID;
                $list_result = $investList->save();
                if(!$list_result){
                    $transaction->rollBack();
                    return $error;
                }
                $init_money = $init_money - $money;
            }else{
                $accrual_one = FinanceRemainder::findOne($id);
                $accrual_one->money = $money - $init_money;
                $one_result = $accrual_one->save();
                if(!$one_result){
                    $transaction->rollBack();
                    return $error;
                }
                $investList = new FinanceRemainderList();
                $investList->staffID = $staffID;
                $investList->adminID = $adminID;
                $investList->remainderID = $id;
                $investList->sendID = $sendModel['id'];
                $investList->money = $init_money;
                $investList->createTime = $now;
                $investList->companyID = $companyID;
                $list_result = $investList->save();
                if(!$list_result){
                    $transaction->rollBack();
                    return $error;
                }
                break;
            }
        }
        //更新用户账号余额
        $reduce = intval($userInfo['reduce']);
        if($reduce > 0 && $companyID == 1){
            $salary = $salary - $reduce;
            $date = date('Ymd');
            $_dayAccrual = new DayAccrual();
            $_dayAccrual->userID = 9;
            $_dayAccrual->date = $date;
            $_dayAccrual->money = $reduce;
            $_dayAccrual->invest = $reduce;
            $_dayAccrual->createTime = $now;
            $_dayAccrual->updateTime = $now;
            $_dayAccrual->typeID = 15;
            $_accrual_result  = $_dayAccrual->save();
            if(!$_accrual_result){
                $transaction->rollBack();
                return $error;
            }
        }
        if($salary <= 0){
            $transaction->commit();
            return ['status'=>1];
        }
        $date = date('Ymd');
        $dayAccrual = new DayAccrual();
        $dayAccrual->userID = $userID;
        $dayAccrual->date = $date;
        $dayAccrual->money = $salary;
        $dayAccrual->invest = $salary;
        $dayAccrual->createTime = $now;
        $dayAccrual->updateTime = $now;
        $dayAccrual->typeID = 15;
        $accrual_result  = $dayAccrual->save();
        if(!$accrual_result){
            $transaction->rollBack();
            return $error;
        }
        //钱包明细
        $user_wallet = new UserWalletLog();
        $user_wallet->type = 1;
        $user_wallet->money = $salary;
        $user_wallet->createTime = $now;
        $user_wallet->userID = $userID;
        $user_wallet->road = 15;
        $user_wallet->desc = '工资';
        $user_wallet_result  = $user_wallet->save();

        if(!$user_wallet_result){
            $transaction->rollBack();
            return $error;
        }
        $transaction->commit();
        return ['status'=>1];
    }

    public static function charge($adminID, $money, $card, $order, $companyID){
        $transaction = \Yii::$app->db->beginTransaction();
        $now = time();
        $error['status'] = 0;
        $error['msg'] = UserMsg::$time_out;
        $money = intval($money * 100);
        try{
            $chargeModel = new FinanceCharge();
            $chargeModel->adminID = $adminID;
            $chargeModel->createTime = $now;
            $chargeModel->card = $card;
            $chargeModel->order = $order;
            $chargeModel->money = $money;
            $chargeModel->companyID = $companyID;
            $charge_result = $chargeModel->save();
            if(!$charge_result){
                $transaction->rollBack();
                return $error;
            }
        }catch (Exception $e){
            $transaction->rollBack();
            $error['msg'] = UserMsg::$already_order;
            return $error;
        }

        $remainderModel = new FinanceRemainder();
        $remainderModel->adminID = $adminID;
        $remainderModel->money = $money;
        $remainderModel->invest = $money;
        $remainderModel->createTime = $now;
        $remainderModel->chargeID = $chargeModel['id'];
        $remainderModel->companyID = $companyID;
        $remainder_result = $remainderModel->save();
        if(!$remainder_result){
            $transaction->rollBack();
            return $error;
        }
        $transaction->commit();
        return ['status'=>1];
    }
}